import json
from datetime import date, datetime


class DateEncoder(json.JSONEncoder):
    def default(self, obj):
        """
        用于json序列化时处理时间字段
        :param obj:
        :return:
        """
        if isinstance(obj, datetime):
            return obj.strftime('%Y-%m-%d %H:%M:%S')
        elif isinstance(obj, date):
            return obj.strftime("%Y-%m-%d")
        else:
            return json.JSONEncoder.default(self, obj)
